package api

import (
	"awesomeProject/internal/service"
	"awesomeProject/pkg/utils"
	"net/http"

	"github.com/gin-gonic/gin"
)

type AIController struct {
	aiService service.AIService
}

func NewAIController(aiService service.AIService) *AIController {
	return &AIController{
		aiService: aiService,
	}
}

// AutoClassifyRequest 自动分类请求
type AutoClassifyRequest struct {
	Title   string `json:"title" binding:"required"`
	Content string `json:"content" binding:"required"`
}

// GenerateSummaryRequest 生成摘要请求
type GenerateSummaryRequest struct {
	Content string `json:"content" binding:"required"`
}

// AnswerQuestionRequest 问答请求
type AnswerQuestionRequest struct {
	Question string `json:"question" binding:"required"`
	Context  string `json:"context"`
}

// GenerateEmbeddingRequest 生成嵌入向量请求
type GenerateEmbeddingRequest struct {
	Text string `json:"text" binding:"required"`
}

// AutoClassify 自动分类文章
// @Summary 自动分类文章
// @Description 根据文章标题和内容自动分类
// @Tags AI
// @Accept json
// @Produce json
// @Param request body AutoClassifyRequest true "分类请求"
// @Success 200 {object} map[string]interface{} "分类结果"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器错误"
// @Router /api/v1/ai/classify [post]
func (ctrl *AIController) AutoClassify(c *gin.Context) {
	var req AutoClassifyRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "Invalid request parameters",
			"code":  "INVALID_PARAMS",
		})
		return
	}

	category, err := ctrl.aiService.AutoClassify(c.Request.Context(), req.Title, req.Content)
	if err != nil {
		utils.HandleError(c, err)
		return
	}

	utils.SuccessResponse(c, gin.H{
		"category": category,
	})
}

// GenerateSummary 生成文章摘要
// @Summary 生成文章摘要
// @Description 根据文章内容生成摘要
// @Tags AI
// @Accept json
// @Produce json
// @Param request body GenerateSummaryRequest true "摘要请求"
// @Success 200 {object} map[string]interface{} "摘要结果"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器错误"
// @Router /api/v1/ai/summary [post]
func (ctrl *AIController) GenerateSummary(c *gin.Context) {
	var req GenerateSummaryRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "Invalid request parameters",
			"code":  "INVALID_PARAMS",
		})
		return
	}

	summary, err := ctrl.aiService.GenerateSummary(c.Request.Context(), req.Content)
	if err != nil {
		utils.HandleError(c, err)
		return
	}

	utils.SuccessResponse(c, gin.H{
		"summary": summary,
	})
}

// AnswerQuestion 问答功能
// @Summary AI问答
// @Description 基于上下文回答问题
// @Tags AI
// @Accept json
// @Produce json
// @Param request body AnswerQuestionRequest true "问答请求"
// @Success 200 {object} map[string]interface{} "回答结果"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器错误"
// @Router /api/v1/ai/qa [post]
func (ctrl *AIController) AnswerQuestion(c *gin.Context) {
	var req AnswerQuestionRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "Invalid request parameters",
			"code":  "INVALID_PARAMS",
		})
		return
	}

	answer, err := ctrl.aiService.AnswerQuestion(c.Request.Context(), req.Question, req.Context)
	if err != nil {
		utils.HandleError(c, err)
		return
	}

	utils.SuccessResponse(c, gin.H{
		"answer": answer,
	})
}

// GenerateEmbedding 生成文本嵌入向量
// @Summary 生成文本嵌入向量
// @Description 将文本转换为嵌入向量
// @Tags AI
// @Accept json
// @Produce json
// @Param request body GenerateEmbeddingRequest true "嵌入向量请求"
// @Success 200 {object} map[string]interface{} "嵌入向量结果"
// @Failure 400 {object} map[string]interface{} "请求参数错误"
// @Failure 500 {object} map[string]interface{} "服务器错误"
// @Router /api/v1/ai/embedding [post]
func (ctrl *AIController) GenerateEmbedding(c *gin.Context) {
	var req GenerateEmbeddingRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"error": "Invalid request parameters",
			"code":  "INVALID_PARAMS",
		})
		return
	}

	embedding, err := ctrl.aiService.GenerateEmbedding(c.Request.Context(), req.Text)
	if err != nil {
		utils.HandleError(c, err)
		return
	}

	utils.SuccessResponse(c, gin.H{
		"embedding": embedding,
		"dimension": len(embedding),
	})
}